﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SodiumHydroxide;
using System.Data.Objects;


namespace TestEF
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void AddElement()
        {
            PublishingCompanyEntities dataModel = new PublishingCompanyEntities();

            int cantInitial = dataModel.Author.Count();

            Author addAuthor = new Author();
            addAuthor.AuthorID = -1;
            addAuthor.FirstName = "test add";
            addAuthor.LastName = "test add";

            dataModel.AddObject("Author", addAuthor);
            int save = dataModel.SaveChanges(true);
            int cantFinal = dataModel.Author.Count();

            Assert.IsTrue(cantFinal > cantInitial);

        }

        [TestMethod]
        public void ListAutoresAll()
        {
            PublishingCompanyEntities dataModel = new PublishingCompanyEntities();
            IList<Author> listQuery = (from o in dataModel.Author
                                       select o).ToList<Author>();

            List<Author> listObject = listQuery.ToList();

            Assert.AreNotEqual(listObject.Count, 0);
        }

        [TestMethod]
        public void ListAutorCondition()
        {
            PublishingCompanyEntities dataModel = new PublishingCompanyEntities();
            IQueryable<Author> query = (from o in dataModel.Author
                                        where o.LastName == "salinas"
                                        select o).AsQueryable<Author>();

            Author author = query.SingleOrDefault<Author>();

            Assert.AreNotEqual(author.AuthorID, 0);
        }

        [TestMethod]
        public void UpdateAuthor()
        {
            PublishingCompanyEntities dataModel = new PublishingCompanyEntities();
            Author update = dataModel.Author.First(x => x.FirstName == "alexis");
            update.FirstName = "Alexis Capo";
            dataModel.SaveChanges();

            Author updateResult = dataModel.Author.First(x => x.FirstName == "Alexis Capo");
            Assert.AreEqual(updateResult.FirstName, "Alexis Capo");

        }

        [TestMethod]
        public void DeleteAuthor()
        {
            PublishingCompanyEntities dataModel = new PublishingCompanyEntities();
            dataModel.DeleteObject(dataModel.Author.First(x => x.FirstName == "test add"));
            dataModel.SaveChanges();
            Author delete = dataModel.Author.SingleOrDefault(x => x.FirstName == "alexis");
            
            Assert.AreEqual(delete, null);
        }
    }
}
